// pages/teacher/teacher.js
const App = getApp()
var util = require('../../utils/util');

const teacherReduceType = {
  0: '正常',
  1: '迟到',
  2: '步骤未完成',
  3: '位置异常',
  4: 'vip签到异常',
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    teacherReduceType: teacherReduceType,
    tabList: [{
        name: '近期课表',
        value: 1
      },
      {
        name: '异常课表',
        value: 2
      },
      {
        name: '课酬',
        value: 3
      },
      {
        name: '学生查询',
        value: 4
      },
    ],
    currentItem: 1,
    date: util.formatTime(new Date()),
    selectMounth: util.currentMonth(),
    month: '',
    totalData: {}, //头部汇总
    recentlyTimetable: [], //近期课表
    abnormalTimetable: [], //异常课表
    salaryList: {}, //课酬列表
    teacherReduceType: teacherReduceType,
    userInfo: {},
    scheduleData: {
      type: '', // 调课类型
      date: util.formatTime(new Date()), //调课时间
      status: false, //调课弹窗
      id: '', //需要调课的课时id
      time: '00:00'
    }, //调课信息
    selectDay: [],
    keyWord: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.rolesTest()
  },
  onShow() {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0
      })
    }
  },
  async rolesTest() {
    // 角色检测
    if (App.globalData.userInfo) {
      // 判断是否是教务或老板
      let role = App.globalData.userInfo.roles[0]
      if (role == 'boss') {
        wx.switchTab({
          url: "/pages/boss/boss"
        });
      } else if (role == 'educational') {
        wx.switchTab({
          url: "/pages/teacherLead/teacherLead"
        });
      } else {
        this.setData({
          userInfo: App.globalData.userInfo.user
        })
        await this.getTeacherData(1)
        await this.getSalarySummary(util.currentMonth())
      }
    } else {
      setTimeout(() => {
        this.rolesTest()
      }, 1000)
    }
  },
  async bindgetdate(e) {
    // 日历日期选择
    let time = e.detail;
    console.log(time)
    this.setData({
      date: `${time.year}-${time.month}-${time.date}`,
      selectMounth: `${time.year}-${time.month}`
    })
    this.getTeacherData(this.data.currentItem, time.isInit)
  },
  bindDateChange(date) {
    this.setData({
      date: date.detail.value,
    })
    this.getTeacherData(this.data.currentItem)
  },
  changeTab: function (item) {
    // 切换
    console.log(item.currentTarget.dataset.iteminfo)
    let iteminfo = item.currentTarget.dataset.iteminfo
    this.setData({
      currentItem: iteminfo.value,
      date: util.formatTime(new Date()),
      month: util.currentMonth(),
      keyWord: "", //学生查询条件
    })
    this.getTeacherData(iteminfo.value)
  },
  getTeacherData(index, isInit = "") {
    switch (index) {
      case 1:
        this.getRecentlyTimetable(isInit)
        break;
      case 2:
        this.getAbnormalTimetable(isInit)
        break;
      case 3:
        this.getSalaryList(isInit)
        break;
      case 4:

        break;
      default:
        break;
    }
  },
  async getSalarySummary() {
    // 老师-查询课酬汇总情况
    let data = await App.$axios.request({
      url: "/teacher/curriculum/salarySummary",
      method: 'GET',
      loading: true,
      data: {
        yearMonth: util.currentMonth()
      }
    })
    if (data) {
      this.setData({
        totalData: data
      })
    }
  },
  async getRecentlyTimetable(isInit) {
    // 老师-近期课表
    await this.getRecentlyTimetableCalendar(isInit)
    let data = await App.$axios.request({
      url: "/teacher/curriculum/recentlyTimetable",
      method: 'GET',
      loading: true,
      data: {
        queryDate: this.data.date
      }
    })
    if (data) {
      this.setData({
        recentlyTimetable: data
      })
    }
  },
  async getAbnormalTimetable(isInit) {
    // 老师-异常课表
    await this.getAbnormalTimetableCalendar(isInit)
    let data = await App.$axios.request({
      url: "/teacher/curriculum/abnormalTimetable",
      method: 'GET',
      loading: true,
      data: {
        queryDate: this.data.date
      }
    })
    if (data) {
      this.setData({
        abnormalTimetable: data
      })
    }
  },
  async getSalaryList() {
    // 老师-课酬列表
    let data = await App.$axios.request({
      url: "/teacher/curriculum/salaryList",
      method: 'GET',
      loading: true,
      data: {
        queryMonth: this.data.month
      }
    })
    if (data) {
      this.setData({
        salaryList: data
      })
    }
  },
  changeMonth(m) {
    // let clickData = m.target.dataset.month
    // let month = this.data.month
    // if (clickData == '-1') {
    //   month = month - 1 > 0 ? month - 1 : 12
    // } else {
    //   month = month + 1 > 12 ? 1 : month + 1
    // }
    this.setData({
      month: m.detail.value
    })
    this.getSalaryList()
  },
  jumpDetail(item) {
    // 跳转详情
    let itemData = item.currentTarget.dataset.iteminfo
    // if (itemData.curriculumStup == 6) return
    if (itemData.scheduleShowButton) {
      console.log(itemData)
      this.setData({
        'scheduleData.status': true,
        'scheduleData.id': itemData.id,
        'scheduleData.time': itemData.startTime,
        'scheduleData.date': itemData.yearMonthDay
      })
      return
    }
    wx.navigateTo({
      url: `/pages/bandDetails/bandDetails?id=${itemData.id}&type=${itemData.curriculumType}`,
    })
  },
  jumpAppeal(item) {
    // 跳转申诉
    let itemData = item.currentTarget.dataset.item
    wx.navigateTo({
      url: `/pages/appeal/appeal?id=${itemData.id}&type=${itemData.curriculumType}`,
    })
  },
  changeSchedule(e) {
    // 调课类型选择
    this.setData({
      'scheduleData.type': e.detail.value
    })
  },
  changeScheduleDate(date) {
    // 选期调课选择时间
    this.setData({
      'scheduleData.date': date.detail.value,
    })
  },
  changeScheduleTime(date) {
    // 选期调课选择时间
    this.setData({
      'scheduleData.time': date.detail.value,
    })
  },
  async scheduleBoxClick(e) {
    //  调课弹窗点击
    if (e.currentTarget.dataset.type == 'ok' && !this.data.scheduleData.type) {
      wx.showToast({
        title: '请先选择调课类型',
        icon: "none"
      })
    } else if (e.currentTarget.dataset.type == 'cancel') {
      this.setData({
        scheduleData: {
          type: '', // 调课类型
          date: util.formatTime(new Date()), //调课时间
          status: false, //调课弹窗
          id: '', //需要调课的课时id
          time: '00:00',
        }
      })
    } else {
      let url //调课请求的url
      let query = {
        // 请求参数
        id: this.data.scheduleData.id
      }
      if (this.data.scheduleData.type == 1) {
        // 顺延调课
        url = '/teacher/vip/scheduleDelay'
      } else if (this.data.scheduleData.type == 2) {
        if (!this.data.scheduleData.date) {
          wx.showToast({
            title: '调课日期不可为空',
            icon: "none"
          })
          return
        }
        // 选日期调课
        query.yearMonthDay = this.data.scheduleData.date
        query.startDateTime = `${this.data.scheduleData.date} ${this.data.scheduleData.time}`
        url = '/teacher/vip/scheduleUpdate'
      }

      let data = await App.$axios.request({
        url: url,
        method: 'PUT',
        loading: true,
        data: query
      })
      if (data === true) {
        this.setData({
          scheduleData: {
            type: '',
            date: util.formatTime(new Date()),
            time: '00:00',
            status: false,
            id: '',
          },
        })
        if (this.data.currentItem == 1) {
          this.getRecentlyTimetable()
        } else if (this.data.currentItem == 4) {
          this.studentQuery()
        }
      }
    }
  },
  async getRecentlyTimetableCalendar(isInit) {
    // 老师-近期课表日历
    if (isInit == 'dayClick') return
    let data = await App.$axios.request({
      url: "/teacher/curriculum/recentlyTimetableCalendar",
      method: 'GET',
      loading: true,
      data: {
        queryMonth: this.data.selectMounth
      }
    })
    if (data) {
      let days = []
      data.forEach(item => {
        if (item.yearMonthDay && item.exitFlag) {
          days.push({
            date: item.yearMonthDay
          })
        }
      })
      this.setData({
        selectDay: days
      })
    }
  },
  async getAbnormalTimetableCalendar(isInit) {
    // 老师-异常课表日历
    if (isInit == 'dayClick') return
    let data = await App.$axios.request({
      url: "/teacher/curriculum/abnormalTimetableCalendar",
      method: 'GET',
      loading: true,
      data: {
        queryMonth: this.data.selectMounth
      }
    })
    if (data) {
      let days = []
      data.forEach(item => {
        if (item.yearMonthDay && item.exitFlag) {
          days.push({
            date: item.yearMonthDay
          })
        }
      })
      this.setData({
        selectDay: days
      })
    }
  },
  changeSearch(e) {
    // 学生查询
    console.log(e)
    this.setData({
      keyWord: e.detail.value
    })
  },
  async studentQuery(e) {
    // 学生查询
    console.log(this.data.keyWord)
    if (!this.data.keyWord) {
      wx.showToast({
        title: '请输入搜索内容',
        icon: "error"
      })
      return
    }
    let data = await App.$axios.request({
      url: "/teacher/curriculum/studentQuery",
      method: 'GET',
      loading: true,
      data: {
        keyWord: this.data.keyWord
      }
    })
    if (data) {
      this.setData({
        studentList: data
      })
    }
  },
  changeTime(e) {
    let item = e.currentTarget.dataset.iteminfo
    console.log(item)
    this.setData({
      'scheduleData.status': true,
      'scheduleData.id': item.id,
      'scheduleData.type': e.currentTarget.dataset.type,
      'scheduleData.time': item.startTime,
      'scheduleData.date': item.yearMonthDay
    })
  },
  async changeStatus(e) {
    let item = e.currentTarget.dataset.iteminfo
    let url
    if (e.currentTarget.dataset.type == 'true') {
      url = "/teacher/curriculum/leave"
    } else {
      url = "/teacher/curriculum/leaveCancel"
    }
    let data = await App.$axios.request({
      url: url,
      method: 'PUT',
      loading: true,
      data: {
        timetableDetailedId: item.id,
        studentUserId: e.currentTarget.dataset.studentid
      }
    })
    if (data) {
      wx.showToast({
        title: '操作成功',
      })
      this.studentQuery()
    }
  }
})